/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.chl.groupofdoom.beans;

import edu.chl.groupofdoom.entities.Torrent;
import edu.chl.groupofdoom.entities.User;
import edu.chl.groupofdoom.jpa.Mapper;
import edu.chl.groupofdoom.support.SupportClass;
import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpServletRequest;

/**
 * Controller bean for torrent searching
 *
 * @author Martin Agfjord
 */
@ManagedBean(name = "SearchBean")
@RequestScoped
public class SearchBean implements Serializable {

    String searchQuery;
    //Searches for 20 torrents when you create the bean
    List<Torrent> result = Mapper.searchForTorrents("", 20);
    List<String> slimListResults;

    /** Creates a new instance of SearchBean */
    public SearchBean() {
    }
    /**
     * Puts the selected torrent into the TorrentBean and redirects to
     * the detailed view of the chosen object
     * @param t
     * @return
     */
    public String viewDetails(Torrent t) {
        System.out.println("Sets currentTorrent to " + t);
        TorrentBean mTorrentBean = SupportClass.getTorrentBean();
        mTorrentBean.setCurrentTorrent(t);
        //The use of id parameter makes it possible to link the url to a friend
        return "viewTorrent.jsf" + "?id=" + t.getId() + "&faces-redirect=true";
    }

    /**
     * Searches for torrents with string searchQuery
     * @param event
     */

    public void performSearch(ActionEvent event) {
        result = Mapper.searchForTorrents(searchQuery, -1);
    }

    /**
     * Searches for name suggestions of torrents
     * 
     * The method uses http-get cause it is used by javascript
     */

    public void performSlimListSearch() {
        HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String slimSearchQuery = req.getParameter("q");

        List<String> result = Mapper.searchForTorrentNames(slimSearchQuery);
        slimListResults = result;
    }

    /**
     * Removes the selected torrent from the database
     * 
     * @param t
     * @return
     */

    public String deleteTorrent(Torrent t) {
        Mapper.removeTorrent(t.getId());
        return "search.jsf" + "?faces-redirect=true";
    }
    /**
     * Retrieves one users uploaded torrents
     * 
     * @param user
     * @return a list of torrents
     */

    public List<Torrent> getUsersTorrents(User user) {
        return Mapper.getUsersTorrents(user);
    }

    public List<Torrent> getResult() {
        return result;
    }

    public void setResult(List<Torrent> result) {
        this.result = result;
    }

    public String getSearchQuery() {
        return searchQuery;
    }

    public void setSearchQuery(String searchQuery) {
        this.searchQuery = searchQuery;
    }

    public List<String> getSlimListResults() {
        return slimListResults;
    }

    public void setSlimListResults(List<String> slimListResults) {
        this.slimListResults = slimListResults;
    }
    
}
